package com.cse.engine;

import java.util.List;

import org.junit.Test;

import com.cse.engine.subEngine.Engine;
import com.mutear.ParallelSearcher.utils.IResult;

/**
 * 静态代理模式</br>
 * 使用引擎类名绑定委托对象
 * @author Mutear
 *
 */
public class EngineProxy implements Engine {
	
	/**
	 * 委托引擎类名
	 */
	private String engineName;
	/**
	 * 委托引擎对象
	 */
	private Engine engine;
	
	public void bind(String engineName){
		if(null != this.engineName && this.engineName.equalsIgnoreCase(engineName)) return;
		this.engineName = engineName;
		
		try {
			Class<?> engineClass = Class.forName(EngineFactory.getEngineFactory().getEngineClassName(engineName));
			engine = (Engine) engineClass.newInstance();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	@Override
	public List<IResult> search(String key) {
		List<IResult> result = null;
		// 前置处理
		
		// 业务逻辑处理
		result = engine.search(key);
		
		// 后置处理
		
		return result;
	}
	
/*	@Test
	public void test(){
		EngineProxy proxy = new EngineProxy();
		proxy.bind("com.cse.engine.subEngine.BaiduEngine");
	}*/

}
